package jmine.tec.web.wicket.cronExpression;

import bancosys.tec.exception.LocalizedMessageHolder;
import bancosys.tec.exception.MessageCreator;
import bancosys.tec.exception.MessageCreatorHelper;

/**
 * Enum that contains types of cron expressions. It's strictly necessary to set the strategy id as 'panelCronExpressionType'.
 * 
 * @author Fabio Sakiyama
 * @created 11/11/2011
 */
public enum CronExpressionTypeEnum implements MessageCreator {

    FIXED_SCHEDULE("jmine.tec.web.wicket.cronexpression.type.fixed.schedule") {
        /**
         * {@inheritDoc}
         */
        @Override
        public CronExpressionTypeStrategy createStrategy() {
            return new FixedScheduleStrategy("panelCronExpressionType");
        }
    }

    ,
    PERIODIC("jmine.tec.web.wicket.cronexpression.type.periodic") {
        /**
         * {@inheritDoc}
         */
        @Override
        public CronExpressionTypeStrategy createStrategy() {
            return new PeriodicStrategy("panelCronExpressionType");
        }
    };

    private final MessageCreator cronExpression;

    /**
     * C'tor.
     * 
     * @param tipoCronKey - cron expression name.
     */
    private CronExpressionTypeEnum(String tipoCronKey) {
        this.cronExpression = MessageCreatorHelper.creator("jmine-tec-web-wicket-messages.properties", tipoCronKey, 0);
    }

    /**
     * Creates a new strategy type of creating cron expression.
     * 
     * @return new strategy type of creating cron expression.
     */
    public abstract CronExpressionTypeStrategy createStrategy();

    /**
     * @return the cronExpression
     */
    public LocalizedMessageHolder getCronExpression() {
        return this.cronExpression.create();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.getCronExpression().getMessage();
    }

    /**
     * {@inheritDoc}
     */
    public LocalizedMessageHolder create(Object... arguments) {
        return this.cronExpression.create(arguments);
    }

    /**
     * {@inheritDoc}
     */
    public int getExpectedArguments() {
        return this.cronExpression.getExpectedArguments();
    }

    /**
     * Retorna <code>true</code> if it's FIXED_SCHEDULE <code>false</code> otherwise
     * 
     * @return <code>true</code> if it's FIXED_SCHEDULE <code>false</code> otherwise
     */
    public boolean isFixedSchedule() {
        return this.equals(FIXED_SCHEDULE);
    }

    /**
     * Retorna <code>true</code> if it's PERIODIC, <code>false</code> otherwise
     * 
     * @return <code>true</code> if it's PERIODIC, <code>false</code> otherwise
     */
    public boolean isPeriodic() {
        return this.equals(PERIODIC);
    }
}
